<?php 

namespace TuxDrink\TuxDb\Drink\Util;

class TuxS3Util {
	
	private $client;
	private $bucket;
	public function __construct($s3) {
/*		$configFile=__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'config-aws.php';
		if (!is_file($configFile)) {
			throw new Exception("aws config file not found");
		}*/
		$this->client=$s3;
	}
	
	public function setBucket($bucket) {
		$this->bucket=$bucket;
	}
	
	
	public function uploadPng($object,$content) {
		$this->client->putObject(array(
				'Bucket'=>$this->bucket,
				'Key'=>$object,
				'Body'=>$content,
				'ACL'        => 'public-read',
				'ContentType'     => 'image/png'
	
		));
	}
	
	public function download($object) {
		$result = $this->client->getObject(array(
				'Bucket' =>$this->bucket,
				'Key' => $object
		));
		return $result['Body'];
	}
	
	public function delete($object) {
		$this->client->deleteObject(array(
				'Bucket' =>$this->bucket,
				'Key' => $object				
		));
	}
	
	public function downloadByUrl($url) {
		$urlSplitted=explode('/', $url);
		$urlSplittedSize=count($urlSplitted);
		$object=$urlSplitted[$urlSplittedSize-2].'/'.$urlSplitted[$urlSplittedSize-1];
		return $this->download($object);
	}
	
	
	
}